/**
 * 时间格式转化
 * @param {object|string|number} time
 * @param {string} cFormat
 * @returns {string|null}
 */
export function parseTime(time, cFormat) {
    if (arguments.length === 0 || !time) return null;
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
    let date;
    if (typeof time === 'object') {
        date = time;
    } else {
        if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
            time = parseInt(time);
        }
        if (typeof time === 'number' && (`${time}`).length === 10) {
            time *= 1000;
        }
        date = new Date(time);
    }
    const fotmatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
    };
    return format.replace(/{([ymdhisa])}/g, (_, key) => {
        const value = fotmatObj[key];
        if (key === 'a') return ['日', '一', '二', '三', '四', '五', '六'][value];
        return value.toString().padStart(2, '0');
    });
}
